<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头部区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>

      <!-- 表单区域 -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="login_form" buttonp>
        <div>
          <p align="center" >黑盒人工智能贷前审批系统</p>
        </div>
        <!-- 用户名 -->
        <el-form-item prop="username">

          <el-input placeholder="请输入用户名" v-model="loginForm.username" prefix-icon="el-icon-user" clearable ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="loginForm.password" prefix-icon="el-icon-lock" :type="pwdType">
            <i slot="suffix" class="el-icon-view" @click="showPwd"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="type">
          <label >用户类型  </label>
          <el-select v-model="loginForm.value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        options: [{
          value: '1',
          label: '内审'
        }, {
          value: '2',
          label: '外审'
        }, {
          value: '3',
          label: '财务'
        }, {
          value: '4',
          label: '管理员'
        }],
        value: '',
        loginForm: {
          username: '',
          password: '',
          value: ''
        },

        loginRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ]
        },

        pwdType: 'password'
      }
    },
    created(){
      localStorage.setItem('username',"")
    },

    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },

      resetLoginForm() {
        this.$refs.loginForm.resetFields()
      },

      login() {
        this.$refs.loginForm.validate(async valid =>{
          if(!valid) return;
          const {data:res} = await this.$http.post('/mlusers/loginMlusers',this.loginForm);
          if(res.state==200) {
            this.$message.success('登录成功!');
            window.sessionStorage.setItem('token',res.token),
            this.$router.push("/baobiao")
          }
          else return this.$message.error('账号或密码错误!');
        });
      }

    }
  }


</script>
<style lang="less" scoped>
  .login_container {
    background: url(../assets/img/bg.png);
    background-size: cover;
    // background-color: #2b4b6b;
    height: 100%;
    position: fixed;
    width: 100%
  }
  p{
    font-weight: bold;
    font-size: 24px;
  }

  .login_box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -68%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .icon-showpassword {
      margin-right: 8px;
    }
  }


</style>
